package com.example.demo.web.controller;

import com.example.demo.bean.Account;
import com.example.demo.service.IAccountService;
import com.example.demo.utils.Message;
import com.example.demo.utils.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/account")
@Validated
public class AccountController {

    @Autowired
    private IAccountService accountService;

    @GetMapping("findAll")
    public Message findAll(){
        List<Account> list = accountService.findAll();
        return MessageUtil.success(list);
    }

    @GetMapping("findById")
    public Message findById(long id){
      Account account=accountService.findById(id);
      return MessageUtil.success(account);
    }

    @DeleteMapping("deleteById")
    public Message deleteById(long id){
        accountService.deleteById(id);
        return MessageUtil.success("删除成功");
    }
    @PostMapping("save")
    public Message save(@NotNull String account, @NotNull String password){
        Account a=new Account();
        a.setAccount(account);
        a.setPassword(password);
        accountService.save(a);
        return MessageUtil.success("保存成功");
    }
}
